<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-menu"></i>{{$route.name}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="handle-box">
            <el-input v-model="select_word" placeholder="用户名" class="search"></el-input>
            <!-- <el-select v-model="value2" placeholder="请选择用户类型" @change="onChange">
                <el-option v-for="(type,index) in userType" :key="index" :label="type.label" :value="type.value"></el-option>
            </el-select> -->
            <el-button type="primary" icon="search" @click="onSearch">搜索</el-button>
        </div>
        <div>
            <el-table :data="list" border fit style="width: 100%">
                <el-table-column prop="id" label="用户id"></el-table-column>
                <el-table-column prop="user_name" label="姓名"></el-table-column>
                <el-table-column prop="role_name" label="角色"></el-table-column>
                <el-table-column prop="user_pwd_mw" label="密码"></el-table-column>
                <el-table-column fixed="right" label="操作">
                    <template slot-scope="scope">
                        <!-- <el-button @click="edit(scope.row)" type="text" size="small">编辑</el-button> -->
                        <el-button type="text" size="small" @click="delUser(scope.row)">删除</el-button>
                        <!-- <el-button type="text" size="small" @click="addPrivilege(scope.row)">添加权限</el-button> -->
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="block">
            <el-pagination @current-change="handleCurrentChange" 
            :current-page="page.number" layout="total, prev, pager, next, jumper" 
            :total="page.total">
            </el-pagination>
        </div>
    </div>
</template>
<script>
import { user } from "../../server/getDate.js";
import { mapState, mapActions } from 'vuex'
import { getStore } from '../../utils/local.js'
import { confirm } from '../../utils/local'
const { frontUser, deleteUser } = user
export default {
    data() {
        return {
          userType: [{
            value: '1',
            label: '智库机构'
          }, {
            value: '2',
            label: '智库专家',
          }, {
            value: '3',
            label: '公司'
          }, {
            value: '4',
            label: '个人'
          }],
          value2: '1',
          statusTable: '',
          select_word: '',
        }
    },
    created() {
        this.getFrontUsers(1)
    },
    computed: mapState({
        page: state => state.FrontUser.page,
        list: state => state.FrontUser.list,
    }),
    methods: {
        ...mapActions(['getList']),
        onChange(value){
            console.log(value)
            this.statusTable = value
        },
        getFrontUsers(number) {
            const params = {
                page: number,
                pageSize: 10,
            }
                if (this.select_word !== '') {
                    params.searchKey = this.select_word
                }
            frontUser({
                ...params
            }).then(res => {
                if (res.code === '000000') {
                    this.getList({
                        ...res,
                        number
                    })
                } else {
                    this.$message.error(res.message)
                }
            }, err => { this.$message.res(err.message)} )
        },
        handleCurrentChange(val) {
            this.getFrontUsers(val)
        },
        onSearch() {
            this.getFrontUsers(this.page.number)
        },
        delUser(row) {
            this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let id = row.id
                deleteUser({ userId: id })
                    .then(res => {
                        if (res.code === '000000') {
                            this.getFrontUsers(this.page.number)
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                        }
                    })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        }
    }
}
</script>
<style lang="scss" scoped>
.handle-box {
  margin-bottom: 1%;
  .search {
    width: 30%;
  }
}

.block {
  text-align: center;
  margin-top: 1%;
}
</style>
